package com.example.demo.common;

import com.example.demo.service.EventPublishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/16.
 */
@Component
public class Scheduler {
    @Autowired
    private EventPublishService eventPublishService;

    @Scheduled(cron="0/5 * * * * ?") //每5秒执行一次
    public void statusCheck() {
        List<Map<String,Object>> lists = eventPublishService.selectAllDataByNew();
        System.out.println("需要发布到MQ的有如下事务数据==========》" + lists);
        if (lists != null && !lists.isEmpty()) {
            try{
                eventPublishService.updateStatus(lists);
            } catch (RuntimeException e) {
                e.printStackTrace();
                System.out.println("发生运行时异常，事务回滚");
            }
        }
    }
}
